/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vanniktech.emoji.sample.slice;

import com.vanniktech.emoji.Actions.EmojIconActions;
import com.vanniktech.emoji.EmojiconRecentsManager;
import com.vanniktech.emoji.EmojiconTextField;
import com.vanniktech.emoji.EmojiconsPopup;
import com.vanniktech.emoji.sample.*;
import net.yslibrary.ohos.sample.keyboardvisibilityevent.KeyboardVisibilityEvent;
import net.yslibrary.ohos.sample.keyboardvisibilityevent.KeyboardVisibilityEventListener;
import net.yslibrary.ohos.sample.keyboardvisibilityevent.Unregistrar;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.utils.Color;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;
import ohos.multimodalinput.event.TouchEvent;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * MainAbilitySlice
 *
 * @since 2021-06-28
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private static final int HEIGHT_EMOJI_KEY_BOARD = 850;

    private static final int BOTTOM_TEXT_FILED = 24;

    private DependentLayout mRootLayout;

    private Image mTitleMore;
    private Image mTitleSwap;
    private Image mTitleMessage;

    private EmojiconTextField textField1;
    private Image mEmojiBtn;
    private Image submit;
    private Checkbox checkBox;
    private EmojIconActions emojIconActions;
    private EmojIconActions emojIconActionsDialog;

    /**
     * 软键盘状态记录标签
     * 0 - 未弹出
     * -1 - 系统软键盘弹出
     * 1 - emoji自定义软键盘弹出
     */
    private int mKeyboardLabel = 0;

    /**
     * 标记Dialog的当前软键盘
     * 0 - 未弹出
     * -1 - 系统软键盘弹出
     * 1 - emoji自定义软键盘弹出
     */
    private int isWhichKey = 0;

    /**
     * 是否需要监听系统软键盘事件
     * true - 调用后需要立即设置成false
     */
    private boolean isNeedKeyListener = false;

    private StackLayout tempC;
    private int height;
    private int width;
    private boolean isScenedClick = true;
    private Button mMaterialBtn;
    private ListContainer mListContainer;
    private TextProvider mTextProvider;
    private DependentLayout mPopLayout;
    private Text mMorePop;
    private DirectionalLayout mSwapPop;
    private DependentLayout mMessageLayout;
    private DependentLayout mMessageDialog;
    private DirectionalLayout mInputLayout;
    private EmojiconTextField mInputDialog;
    private Image mKeyDialog;
    private Image mSendDialog;
    private ListContainer mListContainerDialog;
    private TextDialogProvider mTextDialogProvider;

    private List<String> mTextList = new ArrayList<>();
    private List<String> mTextListDialog = new ArrayList<>();

    private EmojiconRecentsManager mRecentsManager;
    private KeyboardVisibilityEvent mKeyboardVisibilityEvent;
    private Unregistrar mUnregistrar;

    private EventHandler mEventHandler = new EventHandler(EventRunner.getMainEventRunner());

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(getColor(ResourceTable.Color_statusColor));
        getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);

        initData();
        initView();
        initClick();
    }

    private void initData() {
        width = DisplayManager.getInstance().getDefaultDisplay(getContext()).get().getRealAttributes().width;
        height = DisplayManager.getInstance().getDefaultDisplay(getContext()).get().getRealAttributes().height;
        mRecentsManager = EmojiconRecentsManager.getInstance(getContext());
    }

    private void initView() {
        // parent layout
        mRootLayout = (DependentLayout) findComponentById(ResourceTable.Id_root_layout);

        // pop
        mPopLayout = (DependentLayout) findComponentById(ResourceTable.Id_pop_dialog_layout);
        mMorePop = (Text) findComponentById(ResourceTable.Id_pop_text);
        mSwapPop = (DirectionalLayout) findComponentById(ResourceTable.Id_pop_swap);
        Text iosSwap = (Text) findComponentById(ResourceTable.Id_swap_ios);
        Text gooSwap = (Text) findComponentById(ResourceTable.Id_swap_goo);
        Text twitterSwap = (Text) findComponentById(ResourceTable.Id_swap_twitter);
        Text compatSwap = (Text) findComponentById(ResourceTable.Id_swap_compat);
        Text facebookSwap = (Text) findComponentById(ResourceTable.Id_swap_facebook);
        facebookSwap.setClickedListener(this);
        compatSwap.setClickedListener(this);
        twitterSwap.setClickedListener(this);
        gooSwap.setClickedListener(this);
        iosSwap.setClickedListener(this);

        // title
        mTitleMore = (Image) findComponentById(ResourceTable.Id_title_more);
        mTitleSwap = (Image) findComponentById(ResourceTable.Id_title_swap);
        mTitleMessage = (Image) findComponentById(ResourceTable.Id_title_message);

        mMaterialBtn = (Button) findComponentById(ResourceTable.Id_main_ability_material_button);
        mMaterialBtn.setText("\uD83D\uDE18\uD83D\uDE02\uD83E\uDD8C");

        checkBox = (Checkbox) findComponentById(ResourceTable.Id_main_check);
        PixelMapElement pixelMapElement = new PixelMapElement(getPixelMap(ResourceTable.Media_check_no));
        PixelMapElement pixelMapElement1 = new PixelMapElement(getPixelMap(ResourceTable.Media_check_yes));
        StateElement checkElement = new StateElement();
        checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, pixelMapElement1);
        checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, pixelMapElement);
        checkBox.setButtonElement(checkElement);
        checkBox.setChecked(false);
        tempC = (StackLayout) findComponentById(ResourceTable.Id_c);

        mListContainer = (ListContainer) findComponentById(ResourceTable.Id_main_list_container);
        mTextProvider = new TextProvider(this, mTextList);
        mListContainer.setItemProvider(mTextProvider);

        mMessageLayout = (DependentLayout) findComponentById(ResourceTable.Id_message_layout);
        mInputLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_exit_layout);
        mEmojiBtn = (Image) findComponentById(ResourceTable.Id_emoji_btn);
        submit = (Image) findComponentById(ResourceTable.Id_submit_btn);
        textField1 = (EmojiconTextField) findComponentById(ResourceTable.Id_emojicon_text1);
        textField1.setTag(EmojiconsPopup.INSERT_TEXT_TAG);
        textField1.setMaxTextHeight(190);
        setCursorElement(textField1, "#FFFF0000", 28);

        mMessageDialog = (DependentLayout) findComponentById(ResourceTable.Id_dialog_message);
        mInputDialog = (EmojiconTextField) findComponentById(ResourceTable.Id_dialog_input_text);
        mInputDialog.setTag(EmojiconsPopup.INSERT_TEXT_TAG);
        mInputDialog.setMaxTextHeight(190);
        setCursorElement(mInputDialog, "#FFFF0000", 28);
        mKeyDialog = (Image) findComponentById(ResourceTable.Id_dialog_key_icon);
        mSendDialog = (Image) findComponentById(ResourceTable.Id_dialog_send);

        mListContainerDialog = (ListContainer) findComponentById(ResourceTable.Id_message_dialog_list);
        mTextDialogProvider = new TextDialogProvider(this, mTextListDialog);
        mListContainerDialog.setItemProvider(mTextDialogProvider);

        emojIconActions = new EmojIconActions(this, tempC, width, HEIGHT_EMOJI_KEY_BOARD, mRecentsManager, textField1);
        emojIconActionsDialog = new EmojIconActions(this, tempC, width, HEIGHT_EMOJI_KEY_BOARD, mRecentsManager, mInputDialog);
    }

    private void initClick() {
        mEmojiBtn.setClickedListener(this);
        submit.setClickedListener(this);
        mTitleMore.setClickedListener(this);
        mTitleMessage.setClickedListener(this);
        mTitleSwap.setClickedListener(this);
        mMessageLayout.setClickedListener(this);
        mMessageDialog.setClickedListener(this);
        mKeyDialog.setClickedListener(this);
        mSendDialog.setClickedListener(this);
        tempC.setClickedListener(this);
        mPopLayout.setClickedListener(this);

        mMorePop.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                // title more pop
                hide();
                mKeyboardLabel = 0;
                mEmojiBtn.setPixelMap(ResourceTable.Media_emoji_twitter_category_smileysandpeople);
                mPopLayout.setVisibility(Component.HIDE);
                mMorePop.setVisibility(Component.HIDE);

                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withBundleName(getBundleName())
                        .withDeviceId("")
                        .withAbilityName(CustomViewAbility.class.getName())
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
            }
        });

        mKeyboardVisibilityEvent = new KeyboardVisibilityEvent();
        mKeyboardVisibilityEvent.setAbilitySliceRoot(mRootLayout);
        mUnregistrar = mKeyboardVisibilityEvent.registerEventListener(getAbility(), new KeyboardVisibilityEventListener() {
            @Override
            public void onVisibilityChanged(boolean isOpen) {
                if (isNeedKeyListener) {
                    isNeedKeyListener = false;
                    // 首页输入框切换到emoji软键盘
                    if (!isOpen && mKeyboardLabel == 1 && mMessageLayout.getVisibility() != Component.VISIBLE) {
                        show();
                    } else if (!isOpen && isWhichKey == 1 && mMessageLayout.getVisibility() == Component.VISIBLE) {
                        showKeyDialog();
                    }
                } else {
                    if (!isOpen && mMessageLayout.getVisibility() == Component.VISIBLE) {
                        mMessageDialog.setMarginTop(getXpFromDp(230));
                        mTextProvider.notifyDataChanged();
                        isWhichKey = 0;
                    }
                    if (!isOpen && mMessageLayout.getVisibility() != Component.VISIBLE) {
                        mKeyboardLabel = 0;
                        mTextProvider.notifyDataChanged();
                    }
                }
            }
        });

        textField1.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                if (mKeyboardLabel == 1){
                    mListContainer.setMarginBottom(HEIGHT_EMOJI_KEY_BOARD + mInputLayout.getHeight() + BOTTOM_TEXT_FILED);
                } else if (mKeyboardLabel == 0){
                    mListContainer.setMarginBottom(mInputLayout.getHeight() + BOTTOM_TEXT_FILED);
                }
            }
        });

        textField1.setTouchEventListener((component, touchEvent) -> {
            setBaseElement(textField1, "#FF4081");
            switch (touchEvent.getAction()) {
                case TouchEvent.PRIMARY_POINT_DOWN:
                    if (checkBox.isChecked()) {
                        isNeedKeyListener = true;
                        mKeyboardLabel = 1;

                        textField1.setEnabled(false);
                        textField1.clearFocus();
                        show();
                    } else {
                        if (mKeyboardLabel == 1) {
                            textField1.clearFocus();
                            textField1.setEnabled(false);
                        } else if (mKeyboardLabel == 0) {
                            mKeyboardLabel = -1;
                        }
                    }
                    break;
                default:
                    break;
            }
            return true;
        });

        checkBox.setCheckedStateChangedListener((absButton, b) -> {
            if (b) {
                textField1.setEnabled(true);
                mEmojiBtn.setVisibility(Component.HIDE);
            } else {
                mEmojiBtn.setVisibility(Component.VISIBLE);
                if (mKeyboardLabel == 0) {
                    mEmojiBtn.setPixelMap(ResourceTable.Media_emoji_twitter_category_smileysandpeople);
                } else if (mKeyboardLabel == 1) {
                    mEmojiBtn.setPixelMap(ResourceTable.Media_keyboard);
                }
            }
        });

        mInputDialog.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                switch (touchEvent.getAction()) {
                    case TouchEvent.PRIMARY_POINT_UP:
                        if (isWhichKey == 0) {
                            mInputDialog.setEnabled(true);
                            setBaseElement(mInputDialog, "#FF4081");
                            mMessageDialog.setMarginTop(getXpFromDp(100));
                            mKeyDialog.setPixelMap(ResourceTable.Media_emoji_twitter_category_smileysandpeople);
                            hideKeyDialog();
                            isWhichKey = -1;
                        }
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_pop_dialog_layout:
                if (mPopLayout != null && mPopLayout.getVisibility() == Component.VISIBLE) {
                    mPopLayout.setVisibility(Component.HIDE);
                    mMorePop.setVisibility(Component.HIDE);
                    mSwapPop.setVisibility(Component.HIDE);
                }
                break;
            case ResourceTable.Id_title_more:
                // title more
                mPopLayout.setVisibility(Component.VISIBLE);
                mMorePop.setVisibility(Component.VISIBLE);
                mSwapPop.setVisibility(Component.HIDE);
                // 添加阴影效果
                ShadowDrawable.setShadowDrawable(mMorePop, getColor(ResourceTable.Color_white), 10,
                        getColor(ResourceTable.Color_shadow), 10, 0, 0);
                break;
            case ResourceTable.Id_title_message:
                if (mKeyboardLabel == 1) {
                    hide();
                    mTextProvider.notifyDataChanged();
                    mEmojiBtn.setPixelMap(ResourceTable.Media_emoji_twitter_category_smileysandpeople);
                } else {
                    textField1.clearFocus();
                    mListContainer.setMarginBottom(mInputLayout.getHeight() + 30);
                }
                mKeyboardLabel = 0;
                mInputLayout.setEnabled(false);
                mInputLayout.setAlpha(0.5f);
                mListContainer.setEnabled(false);
                mInputLayout.setBackground(null);
                mMessageLayout.setVisibility(Component.VISIBLE);
                break;
            case ResourceTable.Id_title_swap:
                mPopLayout.setVisibility(Component.VISIBLE);
                mSwapPop.setVisibility(Component.VISIBLE);
                mMorePop.setVisibility(Component.HIDE);
                // 添加阴影效果
                ShadowDrawable.setShadowDrawable(mSwapPop, getColor(ResourceTable.Color_white), 10,
                        getColor(ResourceTable.Color_shadow), 10, 0, 0);
                break;
            case ResourceTable.Id_submit_btn:
                // 发送
                if (!textField1.getText().isEmpty() && textField1.getText().length() > 0) {
                    mTextList.add(textField1.getText());
                    mTextProvider.notifyDataSetItemChanged(mTextList.size() - 1);
                    textField1.setText("");
                    mEventHandler.postTask(new Runnable() {
                        @Override
                        public void run() {
                            if (mKeyboardLabel == 1){
                                mListContainer.setMarginBottom(HEIGHT_EMOJI_KEY_BOARD + mInputLayout.getHeight() + BOTTOM_TEXT_FILED);
                            } else if (mKeyboardLabel == 0){
                                mListContainer.setMarginBottom(mInputLayout.getHeight() + BOTTOM_TEXT_FILED);
                            }                        }
                    },100);
                }
                break;
            case ResourceTable.Id_emoji_btn:
                setBaseElement(textField1, "#FF4081");
                // 切换输入法
                if (mKeyboardLabel == 1) {
                    mEmojiBtn.setPixelMap(ResourceTable.Media_emoji_twitter_category_smileysandpeople);

                    mKeyboardLabel = -1;
                    hide();

                    textField1.setFocusable(Component.FOCUS_ENABLE);
                    textField1.setEnabled(true);
                    textField1.requestFocus();
                    textField1.simulateClick();
                } else if (mKeyboardLabel == -1) {
                    isNeedKeyListener = true;
                    mKeyboardLabel = 1;
                    textField1.clearFocus();

                    mEmojiBtn.setPixelMap(ResourceTable.Media_keyboard);
                } else {
                    mEmojiBtn.setPixelMap(ResourceTable.Media_keyboard);

                    mKeyboardLabel = 1;
                    show();
                }
                break;
            case ResourceTable.Id_swap_ios:
            case ResourceTable.Id_swap_goo:
            case ResourceTable.Id_swap_twitter:
            case ResourceTable.Id_swap_compat:
            case ResourceTable.Id_swap_facebook:
                restart();
                break;
            case ResourceTable.Id_message_layout:
                mListContainer.setEnabled(true);
                ShapeElement shapeElement = new ShapeElement();
                shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#FFFFFF")));
                mInputLayout.setBackground(shapeElement);
                mListContainer.setMarginBottom(mInputLayout.getHeight() + BOTTOM_TEXT_FILED);
                hideMessageDialog();
                break;
            case ResourceTable.Id_dialog_message:
                // 仅仅用于拦截父类的点击事件，不需要实现代码
                break;
            case ResourceTable.Id_c:
                // 仅仅用于拦截父类的点击事件，不需要实现代码
                break;
            case ResourceTable.Id_dialog_key_icon:
                setBaseElement(mInputDialog, "#FF4081");
                if (isWhichKey == -1) {
                    mListContainer.setMarginBottom(HEIGHT_EMOJI_KEY_BOARD + mInputLayout.getHeight() + BOTTOM_TEXT_FILED);
                    mKeyDialog.setPixelMap(ResourceTable.Media_keyboard);
                    isWhichKey = 1;

                    mInputDialog.setEnabled(false);
                    mInputDialog.clearFocus();
                    isNeedKeyListener = true;
                } else if (isWhichKey == 1) {
                    mListContainer.setMarginBottom(mInputLayout.getHeight() + BOTTOM_TEXT_FILED);
                    mKeyDialog.setPixelMap(ResourceTable.Media_emoji_twitter_category_smileysandpeople);
                    isWhichKey = -1;

                    // 处理软键盘
                    mInputDialog.setFocusable(Component.FOCUS_ENABLE);
                    mInputDialog.setEnabled(true);
                    mInputDialog.requestFocus();
                    mInputDialog.simulateClick();
                    hideKeyDialog();
                } else {
                    mListContainer.setMarginBottom(HEIGHT_EMOJI_KEY_BOARD + mInputLayout.getHeight() + BOTTOM_TEXT_FILED);
                    mKeyDialog.setPixelMap(ResourceTable.Media_keyboard);
                    isWhichKey = 1;

                    mInputDialog.setEnabled(false);
                    mInputDialog.clearFocus();
                    showKeyDialog();
                }
                break;
            case ResourceTable.Id_dialog_send:
                // 发送
                if (!mInputDialog.getText().isEmpty() && mInputDialog.getText().length() > 0) {
                    mTextListDialog.add(mInputDialog.getText());
                    mTextDialogProvider.notifyDataSetItemChanged(mTextListDialog.size() - 1);
                    mInputDialog.setText("");
                }
                break;
            default:
                break;
        }
    }

    private void show() {
        tempC.setHeight(DependentLayout.LayoutConfig.MATCH_CONTENT);
        emojIconActions.showPopup();
        mListContainer.setMarginBottom(HEIGHT_EMOJI_KEY_BOARD + mInputLayout.getHeight() + 30);
    }

    private void hide() {
        tempC.setHeight(0);
        emojIconActions.hidePopup();
        mListContainer.setMarginBottom(mInputLayout.getHeight() + 30);
    }

    private void showKeyDialog() {
        mMessageDialog.setMarginTop(getXpFromDp(100));
        tempC.setHeight(DependentLayout.LayoutConfig.MATCH_CONTENT);
        emojIconActionsDialog.showPopupFromFloat();
    }

    private void hideKeyDialog() {
        tempC.setHeight(0);
        emojIconActionsDialog.hidePopup();
    }

    private void hideMessageDialog() {
        mKeyDialog.setPixelMap(ResourceTable.Media_emoji_twitter_category_smileysandpeople);
        mInputDialog.clearFocus();
        mInputDialog.setText("");
        mMessageLayout.setVisibility(Component.HIDE);
        mMessageDialog.setMarginTop(getXpFromDp(230));
        mInputLayout.setEnabled(true);
        mInputLayout.setAlpha(1f);
        mTextListDialog.clear();
        mTextDialogProvider.notifyDataChanged();
        isWhichKey = 0;
        hideKeyDialog();
    }

    private PixelMap getPixelMap(int drawableId) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = getResourceManager().getResource(drawableId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            return imageSource.createPixelmap(decodingOptions);
        } catch (IOException | NotExistException e) {
            e.getMessage();
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (IOException e) {
                e.getMessage();
            }
        }
        return null;
    }

    @Override
    public void onActive() {
        super.onActive();
        textField1.clearFocus();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mUnregistrar != null) {
            mUnregistrar.unregister(mRootLayout);
        }
    }

    @Override
    public void onBackPressed() {

        if (mPopLayout != null && mPopLayout.getVisibility() == Component.VISIBLE) {
            mPopLayout.setVisibility(Component.HIDE);
            mMorePop.setVisibility(Component.HIDE);
            mSwapPop.setVisibility(Component.HIDE);
            return;
        }

        mKeyboardLabel = 0;
        if (mMessageLayout.getVisibility() == Component.VISIBLE) {
            if (isWhichKey == 1) {
                // 先收起emoji软键盘
                isWhichKey = 0;
                mMessageDialog.setMarginTop(getXpFromDp(230));
                mKeyDialog.setPixelMap(ResourceTable.Media_emoji_twitter_category_smileysandpeople);
                mInputDialog.setEnabled(true);
                hideKeyDialog();
                mListContainer.setMarginBottom(mInputLayout.getHeight() + BOTTOM_TEXT_FILED);
                return;
            }
            // 再隐藏dialog
            hideMessageDialog();
            return;
        }
        textField1.setEnabled(true);
        if (isScenedClick) {
            isScenedClick = false;
            if (emojIconActions.getVisibility() == Component.VISIBLE) {
                hide();
                mEmojiBtn.setPixelMap(ResourceTable.Media_emoji_twitter_category_smileysandpeople);
                if (mTextList.size() > 0) {
                    mTextProvider.notifyDataChanged();
                }
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isScenedClick = true;
                    }
                }, 1000);
            } else {
                super.onBackPressed();
            }
        } else {
            super.onBackPressed();
        }
    }

    /**
     * dp转xp
     *
     * @param dp dp
     * @return xp
     */
    private int getXpFromDp(int dp) {
        float density = DisplayManager.getInstance().getDefaultDisplay(this).get().getAttributes().densityPixels;
        return (int) (dp * density);
    }

    /**
     * 自定义光标
     *
     * @param textField 输入框
     * @param colorStr  颜色
     * @param height    高度
     */
    private void setCursorElement(TextField textField, String colorStr, float height) {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShape(ShapeElement.LINE); // 设置背景类型
        RgbColor rgbColor = RgbColor.fromArgbInt(Color.getIntColor(colorStr));
        shapeElement.setRgbColors(new RgbColor[]{rgbColor, rgbColor}); // 设置颜色值 起始与结束时的颜色
        shapeElement.setStroke(AttrHelper.vp2px(height, this), rgbColor); // 设置光标高度
        textField.setCursorElement(shapeElement); // 设置光标背景色
    }

    /**
     * 设置输入框的底线颜色
     *
     * @param textField 编辑框
     * @param colorStr 颜色值
     */
    private void setBaseElement(TextField textField, String colorStr) {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor(colorStr)));
        textField.setBasement(shapeElement);
    }
}
